﻿#include "configmanage.h"

configmanage::configmanage()
{
    max_mastercount=10;  //主机列表最大数
    max_slavecount=10;   //客户机列表最大数
    mastercount=0;
    slavecount=0;
    config_path="config/conf";
}

configmanage::~configmanage()
{

}

QMap<QString,QString> configmanage::loadConfig(){
    QFile qf(config_path);
    if(!qf.open(QIODevice::ReadOnly | QIODevice::Text)){
        printf("配置文件不存在！");

    }
    QTextStream in(&qf);
    in.seek(0);
    QString line=in.readLine().trimmed();
    QMap<QString,QString> map;
    while(!line.isNull()){
        if(line.left(1)!="#"){
            QStringList kv= line.split("=");
            map[kv.at(0).trimmed()]=kv.at(1).trimmed();
        }
        line=in.readLine().trimmed();
    }
    qf.close();
    return map;

}
QVector<QString> configmanage::getMasterIp(){
    QMap<QString,QString> map;
    QVector<QString> strArray;
    map=loadConfig();
    for(int i=0;i<max_mastercount;i++){

        QString key="master_ip"+QString::number(i);
        if(!map[key].isNull()){
            int i=strArray.indexOf(map[key]);
            if(i<0){
                strArray.append(map[key]);
                mastercount+=1;
            }

        }

    }

    return strArray;
}
QVector<QString> configmanage::getSlaveIp(){
    QMap<QString,QString> map;
    QVector<QString> strArray;
    map=loadConfig();
    for(int i=0;i<max_slavecount;i++){

        QString key="slave_ip"+QString::number(i);
        if(!map[key].isNull()){
            int i=strArray.indexOf(map[key]);
            if(i<0){
                strArray.append(map[key]);
                slavecount+=1;
            }

        }

    }

    return strArray;
}
int configmanage::getMasterCount(){
    if(mastercount>0){
        return mastercount;
    }else{
        getMasterIp();
        return mastercount;
    }
}

int configmanage::getSlaveCount(){
    if(slavecount>0){
        return slavecount;
    }else{
        getSlaveIp();
        return slavecount;
    }
}
